home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / nuf210.arc / MATCH.C < prev    next >
Text File  |  1990-06-14  |  2KB  |  95 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. static int I_Match (char *Str, char *Pat);
  5. static int S_Match (char *S, char *P, int Anchor);
  6.  
  7.  
  8.  int
  9. Match (char *Str, char *Pat) {
  10.     static char S_Name[66], S_Ext[4];
  11.     static char P_Name[66], P_Ext[4];
  12.     char *p1;
  13.  
  14.     if ( (p1 = strrchr(Str, '\\')) == NULL) p1 = Str;
  15.     else ++p1;
  16.     if ( (p1 = strchr(p1, '.')) != NULL ) {
  17.         *p1 = '\0';
  18.         strcpy(S_Name, Str);
  19.         strcpy(S_Ext, p1+1);
  20.         *p1 = '.';
  21.         }
  22.     else {
  23.         strcpy(S_Name, Str);
  24.         S_Ext[0] = '\0';
  25.         }
  26.  
  27.     if ( (p1 = strrchr(Pat, '\\')) == NULL) p1 = Pat;
  28.     else ++p1;
  29.     if ( (p1 = strchr(p1, '.')) != NULL ) {
  30.         *p1 = '\0';
  31.         strcpy(P_Name, Pat);
  32.         strcpy(P_Ext, p1+1);
  33.         *p1 = '.';
  34.         }
  35.     else {
  36.         strcpy(P_Name, Pat);
  37.         strcpy(P_Ext, "*");
  38.         }
  39.  
  40.     if ( !I_Match(S_Name, P_Name) ) return(0);
  41.     if ( (P_Ext[0] == '\0') && (S_Ext[0] != '\0') ) return(0);
  42.     if ( !I_Match(S_Ext, P_Ext) ) return(0);
  43.     return(1);
  44.     }
  45.  
  46.  
  47.  static int
  48. I_Match (char *Str, char *Pat) {
  49.     char *p, *p1, *p2;
  50.     int t;
  51.  
  52.     if ( (p1 = strchr(Pat, '*')) == NULL)
  53.         return( S_Match(Str, Pat, 1) );
  54.     if (Pat[0] != '*') {
  55.         *p1 = '\0';
  56.         t = S_Match(Str, Pat, 0);
  57.         *p1 = '*';
  58.         if (!t) return(0);
  59.         }
  60.     if (Pat[strlen(Pat)-1] != '*') {
  61.         p2 = strrchr(Pat, '*') + 1;
  62.         if ( !S_Match(&Str[strlen(Str) - strlen(p2)], p2, 1) )
  63.             return(0);
  64.         }
  65.  
  66.     p = Str;
  67.     while ( (p2 = strchr(++p1, '*')) != NULL ) {
  68.         *p2 = '\0';
  69.         while ( (p = strchr(p, p1[0])) != NULL ) {
  70.             if ( S_Match(p, p1, 0) ) break;
  71.             ++p1;
  72.             }
  73.         if (p == NULL) return(0);
  74.         p += strlen(p1);
  75.         *p2 = '*';
  76.         p1 = p2;
  77.         }
  78.     return(1);
  79.     }
  80.  
  81.  
  82.  static int
  83. S_Match (char *S, char *P, int Anchor) {
  84.  
  85.     while (*P != '\0') {
  86.         if ( (*S == *P) || (*P == '?') ) {
  87.             S++;
  88.             P++;
  89.             }
  90.         else return(0);
  91.         }
  92.     if ( Anchor && (*S != '\0') ) return(0);
  93.     return(1);
  94.     }
  95.